package com.nc.delivery_project.server.dao;

import com.nc.delivery_project.server.domain.Currency;
import com.nc.delivery_project.server.domain.Department;
import com.nc.delivery_project.server.domain.LegalPerson;
import com.nc.delivery_project.server.domain.Schedule;

import javax.validation.constraints.NotNull;
import java.sql.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by miroshnik on 1/28/2015.
 * Edition by DRAGON on 28.01.2015
 */
public class ScheduleDao extends AbstractDao<Schedule> {

    protected final static String ENTITY_NAME = Schedule.class.getSimpleName();

    protected final static String SCHEDULE_ID = "SCHEDULE_ID";
    protected final static String ORIGIN_DEPT = "ORIGIN_DEPT";
    protected final static String DESTINATION_DEPT = "DESTINATION_DEPT";
    protected final static String PRIME_COST = "PRIME_COST";
    protected final static String SELLING_RATE = "SELLING_RATE";
    protected final static String DEPARTURE_DATE = "DEPARTURE_DATE";
    protected final static String ARRIVAL_DATE = "ARRIVAL_DATE";
    protected final static String TARIFF_DATE = "TARIFF_DATE";
    protected final static String TRANSPORT_COMPANY = "TRANSPORT_COMPANY";
    protected final static String CURRENCY = "CURRENCY";
    

    public ScheduleDao(String entityName) {
        super(entityName);
    }

    public ScheduleDao(Connection connection) throws SQLException {
        this(connection, ENTITY_NAME);
    }

    public ScheduleDao(Connection connection, String entityName) throws SQLException {
        super(connection, entityName);
    }
    public ScheduleDao(){this(ENTITY_NAME);}


    @Override
    protected void prepareInsertStatement(CallableStatement statement, Schedule object) throws SQLException {
        statement.setString(1, object.getOrigin().getAddress() + "->" + object.getDestination().getAddress());
        statement.registerOutParameter(2, Types.NUMERIC);
        statement.setInt(3, object.getOrigin().getDeptId());
        statement.setInt(4, object.getDestination().getDeptId());
        statement.setString(5, String.valueOf(object.getPrimeCost()));
        statement.setString(6, String.valueOf(object.getSellingRate()));
        statement.setDate(7, new java.sql.Date(object.getDepartureDate().getTime()));
        statement.setDate(8, new java.sql.Date(object.getArrivalDate().getTime()));
        statement.setDate(9, new java.sql.Date(object.getTariffDate().getTime()));
        statement.setInt(10, object.getTransportCompany().getLegalPersonId());
        statement.setInt(11, object.getCurrency().getId());
    }

    @Override
    protected void prepareUpdateStatement(CallableStatement statement, Schedule object) throws SQLException {
        statement.setInt(1, object.getId());
        statement.setInt(2, object.getOrigin().getDeptId());
        statement.setInt(3, object.getDestination().getDeptId());
        statement.setString(4, String.valueOf(object.getPrimeCost()));
        statement.setString(5, String.valueOf(object.getSellingRate()));
        statement.setDate(6, new java.sql.Date(object.getDepartureDate().getTime()));
        statement.setDate(7, new java.sql.Date(object.getArrivalDate().getTime()));
        statement.setDate(8, new java.sql.Date(object.getTariffDate().getTime()));
        statement.setInt(9, object.getTransportCompany().getLegalPersonId());
        statement.setInt(10, object.getCurrency().getId());
    }

    @Override
    protected List<Schedule> parseResultSet(ResultSet rs) throws SQLException {
        List<Schedule> schedules = new ArrayList<>();

        while (rs.next()) {
            int scheduleId = rs.getInt(SCHEDULE_ID);
            int originId = rs.getInt(ORIGIN_DEPT);
            int destinationId = rs.getInt(DESTINATION_DEPT);
            double primeCost = Double.parseDouble(rs.getString(PRIME_COST));
            double sellingRate = Double.parseDouble(rs.getString(SELLING_RATE));
            Date departureDate = rs.getDate(DEPARTURE_DATE);
            Date arrivalDate = rs.getDate(ARRIVAL_DATE);
            Date tariffDate = rs.getDate(TARIFF_DATE);
            int transportCompanyId = rs.getInt(TRANSPORT_COMPANY);
            int currencyId = rs.getInt(CURRENCY);
            Department origin = new Department(originId);
            Department destination = new Department(destinationId);
            LegalPerson transportCompany= new LegalPerson(transportCompanyId);
            Currency currency = new Currency(currencyId);
            schedules.add(new Schedule(scheduleId, origin, destination, primeCost,
                    sellingRate,  departureDate, arrivalDate, tariffDate, transportCompany, currency ));
        }

        return schedules;
    }

}
